package com.sqsf.util;

import com.sqsf.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        session = factory.openSession(true);
        mapper = session.getMapper(UserMapper.class);
    }

    @After
    public void after() {
        session.close();
    }

    @Test
    public void TestConnection() {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            System.out.println("MyBatis配置正确，连接正常");
        } catch (Exception e) {
            System.err.println("MyBatis配置出现问题，连接失败");
            e.printStackTrace();
        }
    }

    @Test
    public void testSelectByUser() {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            User user = new User();
            user.setUsername("张三");
            List<User> users = mapper.selectByUser(user);
            for (User u : users) {
                System.out.println(u);
            }
        }
    }

    @Test
    public void testDeleteByArray() {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            int[] ids = {1, 2, 3};
            int result = mapper.deleteByArray(ids);
            System.out.println("删除了 " + result + " 条记录");
        }
    }
}